package com.ai.service.security.impl;

import com.ai.entity.security.SysPermission;
import com.ai.entity.security.SysUser;
import com.ai.service.security.ISysPermissionService;
import com.ai.service.security.ISysUserService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISysPermissionService sysPermissionService;
    /**
     * String username：用户名
     * String password： 密码
     * boolean enabled： 账号是否可用
     * boolean accountNonExpired：账号是否过期
     * boolean credentialsNonExpired：密码是否过期
     * boolean accountNonLocked：账号是否锁定
     * Collection<? extends GrantedAuthority> authorities)：用户权限列表
     *
     * @param username the username identifying the user whose data is required.
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username == null || "".equals(username)) {
            throw new RuntimeException("用户不能为空");
        }
        //根据用户名查询用户
        SysUser sysUser = sysUserService.selectByName(username);
        if (sysUser == null) {
            throw new RuntimeException("用户不存在");
        }
        log.info("===========================  当前登录用户：{} ===========================", JSONObject.toJSONString(sysUser));
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
            //获取该用户所拥有的权限
            List<SysPermission> sysPermissions = sysPermissionService.selectListByUser(sysUser.getId());
            // 声明用户授权
            if (!sysPermissions.get(0).toString().equals("null")){
                sysPermissions.forEach(sysPermission -> {
                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(sysPermission.getPermissionCode());
                    grantedAuthorities.add(grantedAuthority);
                });
            }
        log.info("===========================  当前用户所拥有的的权限 :{} ===========================",JSONObject.toJSONString(sysPermissions));
        return new User(sysUser.getAccount(), sysUser.getPassword(),
                sysUser.getEnabled(), sysUser.getNotExpired(), sysUser.getCredentialsNotExpired(),
                sysUser.getAccountNotLocked(), grantedAuthorities);
    }
}
